package com.eostoken.eos_wallet.mvp.mode;

import android.app.Activity;
import android.content.Context;

import com.blankj.utilcode.util.LogUtils;
import com.eostoken.eos_wallet.bean.EOSPrice;
import com.eostoken.eos_wallet.eosnet.callback.CheckDataTaskCallback;
import com.eostoken.eos_wallet.eosnet.common.EOSNetWorkManager;
import com.eostoken.eos_wallet.eosnet.common.Params;
import com.eostoken.eos_wallet.eosnet.common.TransactionTask;
import com.eostoken.eos_wallet.mvp.callback.CommonCallback;
import com.eostoken.eos_wallet.net.common.NetWorkManager;
import com.eostoken.eos_wallet.net.common.RxSubscriber;
import com.eostoken.eos_wallet.utils.Constants;
import com.fredy.mvp.Model;
import com.google.gson.JsonObject;

import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

import static com.eostoken.eos_wallet.utils.Constants.ADD_ORDER;
import static com.eostoken.eos_wallet.utils.Constants.BALANCE;
import static com.eostoken.eos_wallet.utils.Constants.BIND_MID;
import static com.eostoken.eos_wallet.utils.Constants.FIND_MID;
import static com.eostoken.eos_wallet.utils.Constants.INDEX_MARK;
import static com.eostoken.eos_wallet.utils.Constants.MORTGAGE;
import static com.eostoken.eos_wallet.utils.Constants.MY_ORDER;
import static com.eostoken.eos_wallet.utils.Constants.NODE_INDEX;
import static com.eostoken.eos_wallet.utils.Constants.ORDER_INDEX;
import static com.eostoken.eos_wallet.utils.Constants.PRICE;
import static com.eostoken.eos_wallet.utils.Constants.TABLE_ROWS;
import static com.eostoken.eos_wallet.utils.Constants.TEALTIME_INDEX;
import static com.eostoken.eos_wallet.utils.Constants.WITHD_RAW_INDEX;

public class AppsModel implements Model {

    public void transaction(String action, String privateKey, String fromAccount, String code, String jsonData, CheckDataTaskCallback callback) {
        new TransactionTask(callback).execute(action, privateKey, fromAccount, code, jsonData);
    }

    public void getOrderIndex(Activity context, String content, int type, boolean showLoading, CommonCallback callback) {
        NetWorkManager.getRequest().getOrderIndex(content)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new RxSubscriber<JsonObject>(context, showLoading) {
                    @Override
                    public void onFail(String failMsg) {
                        LogUtils.dTag("AppsFragment==", "onFail failMsg = " + failMsg);
                        callback.onFailure(failMsg, type);
                    }


                    @Override
                    public void onSuccess(JsonObject jsonObject) {
                        LogUtils.dTag("AppsFragment==", "onSuccess s = " + jsonObject.toString());
                        callback.onSuccess(jsonObject, "", type);
                    }
                });
    }

    public void getFindMgp(Activity context, String content, int type, boolean showLoading, CommonCallback callback) {
        NetWorkManager.getRequest().getFindMgp(content)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new RxSubscriber<JsonObject>(context, showLoading) {
                    @Override
                    public void onFail(String failMsg) {
                        LogUtils.dTag("AppsFragment==", "onFail failMsg = " + failMsg);
                        callback.onFailure(failMsg, type);
                    }


                    @Override
                    public void onSuccess(JsonObject jsonObject) {
                        LogUtils.dTag("AppsFragment==", "onSuccess s = " + jsonObject.toString());
                        callback.onSuccess(jsonObject, "", type);
                    }
                });
    }

    public void getTableRows(Activity context, Map<String, Object> map, boolean showLoading, CommonCallback callback) {
        LogUtils.dTag(Constants.TAG_MGP, "getTableRows map  " + map.toString());
        Observable.fromCallable(() -> EOSNetWorkManager.getRpcProvider().getTableRows(Params.getRequestBody(map)))
                .subscribeOn(Schedulers.newThread())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new RxSubscriber<String>(context, showLoading) {

                    @Override
                    public void onFail(String failMsg) {
                        LogUtils.dTag(Constants.TAG_MGP, "getTableRows onFail: " + failMsg);
                        callback.onFailure(failMsg, TABLE_ROWS);
                    }

                    @Override
                    public void onSuccess(String s) {
                        LogUtils.dTag(Constants.TAG_MGP, "getTableRows onSuccess: " + s);
                        callback.onSuccess(s, "", TABLE_ROWS);
                    }
                });
    }

    public void getCurrencyBalance(Activity context, Map<String, Object> map, boolean showLoading, CommonCallback callback) {
        Observable.fromCallable(() -> EOSNetWorkManager.getRpcProvider().getCurrencyBalance(Params.getRequestBody(map)))
                .subscribeOn(Schedulers.newThread())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new RxSubscriber<String>(context, showLoading) {

                    @Override
                    public void onFail(String failMsg) {
                        LogUtils.dTag(Constants.TAG_MGP, "getCurrencyBalance onFail: " + failMsg);
                        callback.onFailure(failMsg, BALANCE);
                    }

                    @Override
                    public void onSuccess(String s) {
                        LogUtils.dTag(Constants.TAG_MGP, "getCurrencyBalance onSuccess: " + s);
                        callback.onSuccess(s, "", BALANCE);
                    }
                });
    }

    public void getDIGICCYPrice(Activity context, String pair, int type, boolean showLoading, CommonCallback callback) {
        NetWorkManager.getRequest().getDIGICCYPrice(pair)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new RxSubscriber<EOSPrice>(context, showLoading) {
                    @Override
                    public void onFail(String failMsg) {
                        callback.onFailure(failMsg, type);
                    }

                    @Override
                    public void onSuccess(EOSPrice eosPrice) {
                        callback.onSuccess(eosPrice, "", type);
                    }
                });
    }

    public void getRealTimeIndex(Activity context, String content, int type, boolean showLoading, CommonCallback callback) {
        NetWorkManager.getRequest().getRealTimeIndex()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new RxSubscriber<JsonObject>(context, showLoading) {
                    @Override
                    public void onFail(String failMsg) {
                        LogUtils.dTag("AppsFragment==", "onFail failMsg = " + failMsg);
                        callback.onFailure(failMsg, type);
                    }


                    @Override
                    public void onSuccess(JsonObject jsonObject) {
                        LogUtils.dTag("AppsFragment==", "onSuccess s = " + jsonObject.toString());
                        callback.onSuccess(jsonObject, "", type);
                    }
                });
    }

    public void getMortgageAssociation(Activity context, String content, int type, boolean showLoading, CommonCallback callback) {
        NetWorkManager.getRequest().getMortgageAssociation(content)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new RxSubscriber<JsonObject>(context, showLoading) {
                    @Override
                    public void onFail(String failMsg) {
                        LogUtils.dTag("AppsFragment==", "onFail failMsg = " + failMsg);
                        callback.onFailure(failMsg, type);
                    }


                    @Override
                    public void onSuccess(JsonObject jsonObject) {
                        LogUtils.dTag("AppsFragment==", "onSuccess s = " + jsonObject.toString());
                        callback.onSuccess(jsonObject, "", type);
                    }
                });
    }

    public void getBindMidMgp(Activity context, String content, int type, boolean showLoading, CommonCallback callback) {
        NetWorkManager.getRequest().getBindMidMgp(content)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new RxSubscriber<JsonObject>(context, showLoading) {
                    @Override
                    public void onFail(String failMsg) {
                        LogUtils.dTag("AppsFragment==", "onFail failMsg = " + failMsg);
                        callback.onFailure(failMsg, type);
                    }


                    @Override
                    public void onSuccess(JsonObject jsonObject) {
                        LogUtils.dTag("AppsFragment==", "onSuccess s = " + jsonObject.toString());
                        callback.onSuccess(jsonObject, "", type);
                    }
                });
    }

    public void getNodeIndex(Activity context, String content, int type, boolean showLoading, CommonCallback callback) {
        NetWorkManager.getRequest().getNodeIndex(content)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new RxSubscriber<JsonObject>(context, showLoading) {
                    @Override
                    public void onFail(String failMsg) {
                        LogUtils.dTag("AppsFragment==", "onFail failMsg = " + failMsg);
                        callback.onFailure(failMsg, type);
                    }

                    @Override
                    public void onSuccess(JsonObject jsonObject) {
                        LogUtils.dTag("AppsFragment==", "onSuccess s = " + jsonObject.toString());
                        callback.onSuccess(jsonObject, "", type);
                    }
                });
    }

    public void getMyOrderWallet(Activity context, String content, int type, boolean showLoading, CommonCallback callback) {
        NetWorkManager.getRequest().getMyOrderWallet(content)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new RxSubscriber<JsonObject>(context, showLoading) {
                    @Override
                    public void onFail(String failMsg) {
                        LogUtils.dTag("AppsFragment==", "onFail failMsg = " + failMsg);
                        callback.onFailure(failMsg, type);
                    }

                    @Override
                    public void onSuccess(JsonObject jsonObject) {
                        LogUtils.dTag("AppsFragment==", "onSuccess s = " + jsonObject.toString());
                        callback.onSuccess(jsonObject, "", type);
                    }
                });
    }

    public void getWithdrawIndex(Activity context, String content, int type, boolean showLoading, CommonCallback callback) {
        NetWorkManager.getRequest().getWithdrawIndex(content)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new RxSubscriber<JsonObject>(context, showLoading) {
                    @Override
                    public void onFail(String failMsg) {
                        LogUtils.dTag("AppsFragment==", "onFail failMsg = " + failMsg);
                        callback.onFailure(failMsg, type);
                    }

                    @Override
                    public void onSuccess(JsonObject jsonObject) {
                        LogUtils.dTag("AppsFragment==", "onSuccess s = " + jsonObject.toString());
                        callback.onSuccess(jsonObject, "", type);
                    }
                });
    }

    public void getWithdrawAddOrder(Activity context, String content, int type, boolean showLoading, CommonCallback callback) {
        NetWorkManager.getRequest().getWithdrawAddOrder(content)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new RxSubscriber<JsonObject>(context, showLoading) {
                    @Override
                    public void onFail(String failMsg) {
                        LogUtils.dTag("AppsFragment==", "onFail failMsg = " + failMsg);
                        callback.onFailure(failMsg, type);
                    }

                    @Override
                    public void onSuccess(JsonObject jsonObject) {
                        LogUtils.dTag("AppsFragment==", "onSuccess s = " + jsonObject.toString());
                        callback.onSuccess(jsonObject, "", type);
                    }
                });
    }

    public void getIndexMarkIndex(Activity context, String content, int type, boolean showLoading, CommonCallback callback) {
        NetWorkManager.getRequest().getIndexMarkIndex(content)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new RxSubscriber<JsonObject>(context, showLoading) {
                    @Override
                    public void onFail(String failMsg) {
                        LogUtils.dTag("AppsFragment==", "onFail failMsg = " + failMsg);
                        callback.onFailure(failMsg, type);
                    }

                    @Override
                    public void onSuccess(JsonObject jsonObject) {
                        LogUtils.dTag("AppsFragment==", "onSuccess s = " + jsonObject.toString());
                        callback.onSuccess(jsonObject, "", type);
                    }
                });
    }

    public void getBindWalletAddress(Activity context, String content, int type, boolean showLoading, CommonCallback callback) {
        NetWorkManager.getRequest().getBindWalletAddress(content)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new RxSubscriber<JsonObject>(context, showLoading) {
                    @Override
                    public void onFail(String failMsg) {
                        LogUtils.dTag("AppsFragment==", "onFail failMsg = " + failMsg);
                        callback.onFailure(failMsg, type);
                    }

                    @Override
                    public void onSuccess(JsonObject jsonObject) {
                        LogUtils.dTag("AppsFragment==", "onSuccess s = " + jsonObject.toString());
                        callback.onSuccess(jsonObject, "", type);
                    }
                });
    }

    public void bindWalletAddress(Activity context, String content, int type, boolean showLoading, CommonCallback callback) {
        NetWorkManager.getRequest().bindWalletAddress(content)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new RxSubscriber<JsonObject>(context, showLoading) {
                    @Override
                    public void onFail(String failMsg) {
                        LogUtils.dTag("AppsFragment==", "onFail failMsg = " + failMsg);
                        callback.onFailure(failMsg, type);
                    }

                    @Override
                    public void onSuccess(JsonObject jsonObject) {
                        LogUtils.dTag("AppsFragment==", "onSuccess s = " + jsonObject.toString());
                        callback.onSuccess(jsonObject, "", type);
                    }
                });
    }

    public void checkAddr(Activity context, String content, int type, boolean showLoading, CommonCallback callback) {
        NetWorkManager.getRequest().checkAddr(content)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new RxSubscriber<JsonObject>(context, showLoading) {
                    @Override
                    public void onFail(String failMsg) {
                        LogUtils.dTag("AppsFragment==", "onFail failMsg = " + failMsg);
                        callback.onFailure(failMsg, type);
                    }

                    @Override
                    public void onSuccess(JsonObject jsonObject) {
                        LogUtils.dTag("AppsFragment==", "onSuccess s = " + jsonObject.toString());
                        callback.onSuccess(jsonObject, "", type);
                    }
                });
    }

    public void isFindMIDBinding(Activity context, String content, int type, boolean showLoading, CommonCallback callback) {
        NetWorkManager.getRequest().isFindMIDBinding(content)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new RxSubscriber<JsonObject>(context, showLoading) {
                    @Override
                    public void onFail(String failMsg) {
                        LogUtils.dTag("AppsFragment==", "onFail failMsg = " + failMsg);
                        callback.onFailure(failMsg, type);
                    }

                    @Override
                    public void onSuccess(JsonObject jsonObject) {
                        LogUtils.dTag("AppsFragment==", "onSuccess s = " + jsonObject.toString());
                        callback.onSuccess(jsonObject, "", type);
                    }
                });
    }

    public void getMiningOrderIncome(Activity context, String content, int type, boolean showLoading, CommonCallback callback) {
        NetWorkManager.getRequest().getMiningOrderIncome(content)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new RxSubscriber<JsonObject>(context, showLoading) {
                    @Override
                    public void onFail(String failMsg) {
                        LogUtils.dTag("AppsFragment==", "onFail failMsg = " + failMsg);
                        callback.onFailure(failMsg, type);
                    }

                    @Override
                    public void onSuccess(JsonObject jsonObject) {
                        LogUtils.dTag("AppsFragment==", "onSuccess s = " + jsonObject.toString());
                        callback.onSuccess(jsonObject, "", type);
                    }
                });
    }
}
